﻿using FitnessClubManager.BLL;
using FitnessClubManager.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace FitnessClubManager.GUI
{
    public partial class FormQuanLyKhachHang : FormBase
    {
        public void SetReadOnlyGrid()
        {
            gridKhachHang.AllowUserToAddRows = false;
            gridKhachHang.AllowUserToDeleteRows = false;
            gridKhachHang.ReadOnly = true;
            gridKhachHang.AutoGenerateColumns = false;

            gridLichSuGiaoDich_CheckCardTab.AllowUserToAddRows = false;
            gridLichSuGiaoDich_CheckCardTab.AllowUserToDeleteRows = false;
            gridLichSuGiaoDich_CheckCardTab.ReadOnly = true;
            gridLichSuGiaoDich_CheckCardTab.AutoGenerateColumns = false;

        }

        public FormQuanLyKhachHang()
        {
            InitializeComponent();

            SetReadOnlyGrid();
        }

        private void OnLoad(object sender, EventArgs e)
        {

        }

        //int rowIndexToolTip = -1;
        private void gridPhan_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
        }

        private void bsHocPhan_DataSourceChanged(object sender, EventArgs e)
        {
            lbHocPhan_ThongKe.Text = "Danh sách này có " + bsLichSuGiaoDich.Count.ToString() + " học phần";
        }

        private void txtHoTenKhachHang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                bsKhachHang.DataSource = null;
                var ls = BizKhachHang.GetList(txtHoTenKhachHang.Text);
                if (ls != null && ls.Count > 0)
                {
                    bsKhachHang.DataSource = ls;
                    gridKhachHang.DataSource = bsKhachHang;
                }
            }
        }

        private void bsKhachHang_CurrentChanged(object sender, EventArgs e)
        {
            bsLichSuGiaoDich.DataSource = null;
            if (bsKhachHang.Current != null)
            {
                var khachHang = bsKhachHang.Current as KhachHang;
                if (khachHang != null)
                {
                    lbHoTen.Text = "Họ tên: " + khachHang.Ho + " " + khachHang.Ten;
                    lbSoDienThoai.Text = string.Format("Số điện thoại: {0}", khachHang.SoDienThoai);
                    lbDiaChi.Text = string.Format("Địa chỉ: {0}", khachHang.DiaChi);
                    bsLichSuGiaoDich.DataSource = BizLichSuGiaoDich.GetHistory(khachHang.MaKhachHang);
                    gridLichSuGiaoDich_CheckCardTab.DataSource = bsLichSuGiaoDich;
                }
            }
        }

        private void bsLichSuGiaoDich_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void pnlHeader_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gridKhachHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex == -1)
            {
                if (MessageBox.Show("Bạn có thực sự muốn xóa khách hàng này", "Chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    if (bsKhachHang.Current != null)
                    {
                        var khachHang = bsKhachHang.Current as KhachHang;
                        if (khachHang != null)
                        {
                            if (BizKhachHang.Delete(khachHang.MaKhachHang))
                            {
                                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                bsKhachHang.RemoveCurrent();
                            }
                        }
                    }
                }
            }
        }
    }
}